﻿#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_main.h"
#include "ui_set.h"
#include "ui_mask.h"
#include "ui_test.h"
#include <vector>
#include <map>
#include <regex>
#include "msg_type.h"
#include "setup.pb.h"
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QDoubleValidator>
#include <QIntValidator>
#include <QMetaType>
#include <QTimer>
#include <QProgressDialog>

#include "QSpacerItem"
class set_arg_widget
{
public:
    QLabel* lab{ nullptr };
    QLineEdit* edit{ nullptr };
    QPushButton* set{ nullptr };
    QSpacerItem* spacer{nullptr};
};
class mask_t
{
public:
    mask_t()
    {
        set_mask_stamp = -1;
        set_mask_pos = -1;
    }
    int64_t set_mask_stamp;//上一次屏蔽的时间
    int32_t set_mask_pos;//上一次屏蔽的位置
};
class mqtt_file_t
{
public:
	int total_len;
	int  len;
	char name[256]{ 0 };
	int total_pack;
	int cur_pack;
	char md5[128] = { 0 };
	int trans_flag{ 1 };//1表示开始 2表示进行中 3表示结束
};
class desk : public QMainWindow
{
    Q_OBJECT;
public:
    desk(QWidget* parent = nullptr);
    void Init();
    void MenuInit();
    void LoadSetUpFile();
    void ImageInit();
    void RefreshUiData();
    void SubscribeData();
    void MakeMission(msgType::pb_run_test_item&);
private:
    Ui::PositionClass ui;
    Ui::SettingWindow set_ui;
    Ui::MaskForm mask_ui;
    Ui::TestForm test_ui;
    QMainWindow set_win;
    QMainWindow mask_win;
    QMainWindow test_win;
private slots:
    void slot_OnButton();
    void slot_OnPressButton();
    void slot_OnReleaseButton();
    void slot_OnMenu(QAction*);
    void slot_AddWidget(QString, QString);
    void slot_DelWidget();
    void slot_Alert(QString);
    void slot_Alert(QString, int);
    void slot_Ack(QString);
    void OnEnter(int);
    void OnLeave(int);
signals:
    void sig_AddWidget(QString, QString);
    void sig_DelWidget();
    void sig_UpdateProcess(int, int);
    void sig_Alert(QString);
    void sig_Alert(QString, int);
    void sig_Ack(QString);
private:
    int run_motor_speed_{ 400 };
    int height_motor_speed_{ 1000 };
    int64_t stamp_{ 0 };
    msgType::pb_run_bms bms_;
    msgType::pb_encoder back_;
    msgType::pb_encoder runa_;
    msgType::pb_encoder runb_;
    msgType::pb_encoder height_;
    msgType::pb_encoder rop_motor_;
    msgType::pb_encoder line_motor_;
    msgType::pb_io up_limit_io_;
    msgType::pb_io left_limit_io_;
    msgType::pb_io right_limit_io_;
    msgType::pb_dev_state state_;
    std::regex re_{ "test/data/pb_bat/(.*)" };
    std::regex adc_re_{ "test/data/pb_dev_work_sta/(.*)" };
    std::regex io_re_{ "test/data/pb_io/(.*)" };
    std::vector<set_arg_widget*> set_widgets_;
    std::map<std::string, std::string> advance_set_;
    QGridLayout* set1_lay_{ nullptr };
    QGridLayout* set2_lay_{ nullptr };
    QGridLayout* set3_lay_{ nullptr };
    QLabel* lab_up{ nullptr };
    QLabel* lab_down{ nullptr };
    QLabel* lab_status{ nullptr };
    QLabel* lab_ack{ nullptr };
    QAction* action_upgrade_;
    QAction* action_restore_;
    QAction* action_args_;
    QDoubleValidator* validator_;
    QProgressDialog process_dialog_;
    QString up_version_;
    QString down_version_;
	QTimer timer_shake_;
	QMenu menu_;
	QMainWindow pop_win_{ this };
	QLabel lab_err_;
	QStringList err_show[5];

	bool process_dialog_flag_{ false };
    bool link_{ false };
    int  height_sta_{ 1 };//停止  2上升   3下降
    bool update_flag_{ false };
    bool timer_shake_flag_{ false };
    bool timer_con_flag_{ false };
    bool restore_run_{ false };
    int right_pos_;
    int shake_time_{ 0 };
    int  trans_bytes_;
	int  err_link_[5]{ 0,0,0,0,0 };
    int64_t factory_tick_{ 0 };
    mqtt_file_t mqtt_trans_;
	mask_t mask_run_;
    const int pack_len_{102400};
    MyLabel*  err_label_[5];
};
